/*
========================================================================
 Name        : PublicTransportMapContainer.h
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
#ifndef PUBLICTRANSPORTMAPCONTAINER_H
#define PUBLICTRANSPORTMAPCONTAINER_H

// [[[ begin generated region: do not modify [Generated Includes]
#include <coecntrl.h>		
// ]]] end generated region [Generated Includes]

#include <ImageConversion.h>
#include "CActiveHelper.h"
#include "lbs.h"
#include "publictransporttilerequest.h"
#include "IapConnectionManager.h"
#include "SingaporeBusStructures.h"
#include <euserhl.h>
#include <akniconutils.h>

#if !defined(__SERIES60_3X__) || defined(__S60_50__)
#include <akntoolbarobserver.h>
#include <akntoolbar.h>
#include <aknlongtapdetector.h>
#include <akntitlepaneobserver.h>
#endif


// [[[ begin [Event Handler Includes]
// ]]] end [Event Handler Includes]


// [[[ begin generated region: do not modify [Generated Forward Declarations]
class MEikCommandObserver;		
// ]]] end generated region [Generated Forward Declarations]


/**
 * Container class for PublicTransportMapContainer
 * 
 * @class	CPublicTransportMapContainer PublicTransportMapContainer.h
 */
class CPublicTransportMapContainer : public CCoeControl
#if !defined(__SERIES60_3X__) || defined(__S60_50__)
, public MAknToolbarObserver
, public MAknLongTapDetectorCallBack
, public MAknTitlePaneObserver
#endif
	{
#if !defined(__SERIES60_3X__) || defined(__S60_50__)
	// http://www.developer.nokia.com/Community/Wiki/Working_with_LongTapDetector_API_-_S60_Touch_UI
	LManagedPtr<CAknLongTapDetector> iLongTapDetector;
	TBool iLongTapUnderWay;
	virtual void HandleLongTapEventL(const TPoint& aPenEventLocation, const TPoint& aPenEventScreenLocation);
	TBool LongTapPointerEventL(const TPointerEvent& aPointerEvent);
    virtual void HandleTitlePaneEventL( TInt aEventID );
#endif
	void OfferToolbarEventL(TInt aCommand);
public:
	// constructors and destructor
	CPublicTransportMapContainer();
	static CPublicTransportMapContainer* NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver );
	static CPublicTransportMapContainer* NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver );
	void ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver );
	virtual ~CPublicTransportMapContainer();

public:
	
	mutable TInt iDrawThreadId, iDrawBackThreadId, iEmptyIconCount, iZeroVisibleStationsCount;
	mutable TInt iCountMismatch;
	
	void CenterMapOnBusStop(TUint aBusStop);
	void CenterMapOn(const TCoordinate& aCoord);
	
	// from base class CCoeControl
	TInt CountComponentControls() const;
	CCoeControl* ComponentControl( TInt aIndex ) const;
	TKeyResponse OfferKeyEventL( 
			const TKeyEvent& aKeyEvent, 
			TEventCode aType );
	void HandleResourceChange( TInt aType );
	
	LManagedPtr<CGulIcon> iStarIcon;
	LManagedPtr<CGulIcon> iFlagOrigIcon;
	LManagedPtr<CGulIcon> iFlagDestIcon;
	LManagedPtr<CGulIcon> iBusStationIcon;
	LManagedPtr<CGulIcon> iMrtStationIcon;
	
	TBool iPositionTracking;
	
	CGulIcon* LoadAndScaleIconL(const TDesC& aFileName, TInt aBitmapId,  TInt aMaskId,  TSize* aSize, TScaleMode aScaleMode );

	TUint64 iThreadId;
	
	LManagedPtr<CAsyncCallBack> iAsyncDownloadVisibleTilesCallback;
	
	
	RArray<const TStation*> iVisibleStations;
	void RefreshVisibleStationsL();
	const TStation* FindClosestVisibleStationL(const TCoordinate& aPos);
	const TStation* FindNextVisibleStationL(const TStation* aFromStation, const TCoordinate& aDirection);
	
	TBool LoadCurrentDisplayServiceL();
	
	//TCoordinate iOrig;
	double iTilesResolution;
	TSize iTilesSize;
	
	const CFont* iMapFont;	
	
	void ZoomTo(TInt aZoomLevel);
	
	
	TPoint iDragStart;
	TTime iLastEButton1Down;
	TBool iDragging;
	const TStation* iSelectedStationBeforeLastEButton1Down;
	void HandlePointerEventL(const TPointerEvent& aPointerEvent);
	
	TTime iDragTimes[2];
	TPoint iDragPositions[2];
	TPoint iFreeScrollSpeed, iOrigFreeScrollSpeed; // speed is in pixel per 100s
	TPoint (*iFreeScrollFunction)(const TPoint& aCurrentSpeed, const TPoint& aOrigSpeed, const TInt64& aMicroSeconds);
	LManagedPtr<CPeriodic> iFreeScrollPeriodic;
	TInt FreeScrollCallback();
	TTime iFreeScrollCallbackTime;
	
		
	mutable TRect iServiceNameRect;
	mutable TRect iZoomInRect;
	mutable TRect iZoomOutRect;
	mutable TRect iCenterMapOnPositionRect;
	mutable TRect iRefreshPositionRect;
	
	LManagedPtr<CGulIcon> iZoomInIcon;
	LManagedPtr<CGulIcon> iZoomOutIcon;
	LManagedPtr<CGulIcon> iCenterMapOnPositionIcon;
	LManagedPtr<CGulIcon> iRefreshPositionIcon;

	
    // for double buffering
	LManagedPtr<CFbsBitmap> iBackBuffer; 
	LManagedPtr<CFbsBitmapDevice> iBackBufferDevice;
	LManagedPtr<CFbsBitGc> iBackBufferContext;
	TSize iBackBufferSize;	
	void DrawToBackBuffer();
	void DrawToBackBufferCore();
	void DrawToCore(CBitmapContext* aGc) const;
	
protected:
	// from base class CCoeControl
	void SizeChanged();

private:
	// from base class CCoeControl
	void Draw( const TRect& aRect ) const;

private:
	void InitializeControlsL();
	void LayoutControls();
	CCoeControl* iFocusControl;
	MEikCommandObserver* iCommandObserver;
	// [[[ begin generated region: do not modify [Generated Methods]
public: 
	// ]]] end generated region [Generated Methods]
	
	// [[[ begin generated region: do not modify [Generated Type Declarations]
public: 
	// ]]] end generated region [Generated Type Declarations]
	
	// [[[ begin generated region: do not modify [Generated Instance Variables]
private: 
	// ]]] end generated region [Generated Instance Variables]
	
	
	// [[[ begin [Overridden Methods]
protected: 
	// ]]] end [Overridden Methods]
	
	
	// [[[ begin [User Handlers]
protected: 
	TKeyResponse HandlePublicTransportMapContainerOfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType );
	// ]]] end [User Handlers]
	
public: 
	enum TControls
		{
		// [[[ begin generated region: do not modify [Generated Contents]
		
		// ]]] end generated region [Generated Contents]
		
		// add any user-defined entries here...
		
		ELastControl
		};
	};
				
#endif // PUBLICTRANSPORTMAPCONTAINER_H
